<?php

namespace App\Models\CacheModel\chat\hz;

use app\common\lib\Util;
use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class HzChatRecommendQuestion extends BaseCache
{
    protected $table = 'haozhungpt_api.hz_chat_recommend_question';

    protected $cacheModel = 'chat.recommend_question';


    /**
     * 获取 对话常用问题
     * @param int $type 1=>获取对话推荐问题 2=>获取对话常用问题
     * @return array
     * @throws \Exception
     */
    public function getRecommendQuestion(int $type, int $rid)
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $type)->setSort('DESC')->ZsGetIocData('list', function () use ($type)
        {
            try
            {
                $where = ['status' => 1];
                if ($type === 1)
                {
                    $where['is_recommend'] = 1;
                }
                $data = $this->where($where)->order('sort desc')->column('sort', 'id');
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        }, true);

        $ids = array_keys($ids);
        $dataList = $this->batchGetInfoByIds($ids);
        $tagInfos = (new HzTag())->allTag();
        if ($type === 1)
        {
            if (isset($tagInfos[11]))
            {
                $tagInfos[5]['is_recommend'] = 0;
            }

            $tagInfos = array_filter($tagInfos, static function ($item)
            {
                return (int)$item['is_recommend'] === 1;
            });
        }
        $allTags = [
            'id' => 0,
            'tag_type' => 0,
            'tag_name' => '全部',
            'remark' => '全部',
            'tag_icon' => '全部',
            'list' => [],
            'sort' => 99999,
        ];
        foreach ($ids as $idItem)
        {
            if (isset($dataList[$idItem], $tagInfos[$dataList[$idItem]['tid']]))
            {
                $tmp = [
                    'id' => $dataList[$idItem]['id'],
                    'question' => $dataList[$idItem]['question'],
                    'replace_list' => $dataList[$idItem]['replace_data'],
                    'is_recode' => $dataList[$idItem]['is_recode'],
                    'sort' => $dataList[$idItem]['sort'],
                ];
                $allTags['list'][] = $tmp;
                $tagInfos[$dataList[$idItem]['tid']]['list'][] = $tmp;
            }
        }
        //常用推荐 全部类型
        if ($type === 2)
        {
            array_unshift($tagInfos, $allTags);
        }
        $sortTag = array_column($tagInfos, 'sort');
        array_multisort($sortTag, SORT_DESC, $tagInfos);
        $resData = [];
        foreach ($tagInfos as $tid => $dataItem)
        {
            if ($dataItem['tag_name'] === '双人合盘' && Util::IsHuaweiOrHonor()) // 2024-09-26 修改 文案一直都变 不单单是审核模式
            {
                $dataItem['tag_name'] = '双人关系';
            }
            if ($type === 1)
            {   // 1=>获取对话推荐问题
                $randIndex = array_rand($dataItem['list'], 1);
                $d = [
                    'icon' => 'https://oss-haozhun.goddessxzns.com/' . $dataItem['tag_icon'],
                    'title' => $dataItem['tag_name'],
                    'question' => $dataItem['list'][$randIndex]['question'] ?? '',
                    'replace_list' => $dataItem['list'][$randIndex]['replace_list'] ?? [],
                    'is_recode' => $dataItem['list'][$randIndex]['is_recode'] ?? 0,
                ];
                if ($tid === 6 && !$rid)
                {
                    array_unshift($resData, $d);
                } else
                {
                    $resData[] = $d;
                }
            } else
            {   // 2=>获取对话常用问题
                $resData[] = [
                    'tab_name' => $dataItem['tag_name'],
                    'question_list' => $dataItem['list'] ?? [],
                ];
            }
        }
        if (!$rid)
        {
            $index = array_search('星座解析', array_column($resData, 'title'));
            if ($index)
            {
                array_splice($resData, 0, 0, array_splice($resData, $index, 1));
            }
        }
        return $resData;
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['id' => ['in', $ids]];
        $field = 'tid,question,status,sort,replace_data,is_recode,is_recommend,create_time,update_time';
        try
        {
            $data = $this
                ->field($field)
                ->where($where)
                ->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $orderId => $orderItemData)
        {
            $resData[$orderId] = arrayKeyData2int($orderItemData, ['id', 'tid', 'is_recode', 'sort', 'status', 'is_recommend', 'create_time', 'update_time']);

            if (isset($orderItemData['replace_data']))
            {
                $resData[$orderId]['replace_data'] = json_decode($orderItemData['replace_data'], true);
            }
        }

        return $resData;
    }
}
